<template>
  <!-- //!绘制弹框 -->
  <el-dialog :title="title" :visible.sync="dialogShow" @close="closeDialog">
    <!-- //!表单绘制 -->
    <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="是否可用" prop="enVisible">
        <!-- 1显示 0隐藏 -->
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="">
        <el-button size="small" @click="dialogShow = false">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  addPermissionApi,
  editPermissionApi,
  getPermissionInfoApi
} from '@/api/permission'
export default {
  name: 'AddPerm',
  data() {
    return {
      dialogShow: false,
      formData: {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: '', // 权限类型 1菜单权限 2按钮权限
        pid: '' // 上级ID是谁
      },
      formRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        code: [{ required: true, message: '请输入', trigger: 'blur' }],
        description: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  methods: {
    async btnOk() {
      try {
        await this.$refs.form.validate()
      } catch (e) {
        return console.log(e)
      }
      if (this.formData.id) {
        await editPermissionApi(this.formData)
        this.$message.success('编辑成功')
      } else {
        await addPermissionApi(this.formData)
        this.$message.success('新增成功')
      }

      this.$emit('updateList')
      this.dialogShow = false
    },
    closeDialog() {
      this.formData = {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: '', // 权限类型 1菜单权限 2按钮权限
        pid: '' // 上级ID是谁
      }
      this.$refs.form.clearValidate()
    },
    // 回显逻辑
    async getRowInfo(id) {
      const res = await getPermissionInfoApi(id)
      console.log(res)
      this.formData = res
      this.dialogShow = true
    }
  }
}
</script>

<style></style>
